package com.meteor.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meteor.framework.common.enums.UserAdminEnum;
import com.meteor.framework.common.exception.ServerException;
import com.meteor.framework.security.user.SecurityUser;
import com.meteor.user.convert.SysRoleConvert;
import com.meteor.user.entity.SysPost;
import com.meteor.user.entity.SysRole;
import com.meteor.user.query.SysPostAndOrgQuery;
import com.meteor.user.service.SysMerchantService;
import com.meteor.user.service.SysOrgService;
import com.meteor.user.vo.SysMerchantVO;
import com.meteor.user.vo.SysRoleVO;
import lombok.AllArgsConstructor;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.mybatis.service.impl.BaseServiceImpl;
import com.meteor.user.convert.SysPostConvert;
import com.meteor.user.mapper.SysPostMapper;
import com.meteor.user.query.SysPostQuery;
import com.meteor.user.service.SysPostService;
import com.meteor.user.service.SysUserPostService;
import com.meteor.user.vo.SysPostVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 岗位管理
 *
 * @author lynn
 * @date 2024/1/03
 */
@Service
@AllArgsConstructor
public class SysPostServiceImpl extends BaseServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    private final SysUserPostService sysUserPostService;
    private final SysMerchantService sysMerchantService;
    private final SysOrgService sysOrgService;

    @Override
    public PageResult<SysPostVO> page(SysPostQuery query) {

        if(!ObjectUtils.isEmpty(query.getOrgId())){
            query.setOrgIds(sysOrgService.getSubOrgIdList(query.getOrgId()));
        }

        if(SecurityUser.getUser().getUsername().equalsIgnoreCase(UserAdminEnum.ADMIN.getValue())){
            query.setSuperAdmin(1);
        }

        IPage<SysPost> page1 = baseMapper.selectPage(getPage(query), getWrapper(query));

//        Page<SysPostVO> page1 = baseMapper.page(getPage(query),query);

        return new PageResult<>(SysPostConvert.INSTANCE.convertList(page1.getRecords()), page1.getTotal());
    }

    @Override
    public List<SysPostVO> getList() {
        SysPostQuery query = new SysPostQuery();
        //正常岗位列表
        query.setStatus(1);
        List<SysPost> entityList = baseMapper.selectList(getWrapper(query));

        return SysPostConvert.INSTANCE.convertList(entityList);
    }

    private Wrapper<SysPost> getWrapper(SysPostQuery query){
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getPostCode()), SysPost::getPostCode, query.getPostCode());
        wrapper.like(StrUtil.isNotBlank(query.getPostName()), SysPost::getPostName, query.getPostName());
        wrapper.eq(query.getStatus() != null, SysPost::getStatus, query.getStatus());
        wrapper.eq(StrUtil.isNotBlank(query.getMerchantId()), SysPost::getMerchantId, query.getMerchantId());
        wrapper.in(!ObjectUtils.isEmpty(query.getOrgIds()), SysPost::getOrgId, query.getOrgIds());
        wrapper.eq(StrUtil.isNotBlank(query.getDepartmentId()), SysPost::getDepartmentId, query.getDepartmentId());
        wrapper.orderByAsc(SysPost::getSort);

        // 数据权限
        dataScopeWrapper(wrapper);

        return wrapper;
    }

    @Override
    public void save(SysPostVO vo) {
        SysPost entity = SysPostConvert.INSTANCE.convert(vo);

        if(!SecurityUser.getUser().getUsername().equalsIgnoreCase(UserAdminEnum.ADMIN.getValue())){
            if(ObjectUtils.isEmpty(vo.getMerchantId())){
                entity.setMerchantId(SecurityUser.getUser().getMerchantId());
            }

            if(ObjectUtils.isEmpty(vo.getOrgId())){
                entity.setOrgId(SecurityUser.getUser().getOrgId());
            }
        }else{
            if(ObjectUtils.isEmpty(vo.getOrgId())) {
                throw new ServerException("请选择机构");
            }
            SysMerchantVO sysMerchantVO = sysMerchantService.orgMerchant(vo.getOrgId());
            if(!ObjectUtils.isEmpty(sysMerchantVO)){
                entity.setMerchantId(sysMerchantVO.getId());
            }

            /*if(ObjectUtils.isEmpty(vo.getMerchantId())) {
                throw new ServerException("请选择商户");
            }*/
        }

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysPostVO vo) {
        SysPost entity = SysPostConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        // 删除岗位
        removeByIds(idList);

        // 删除岗位用户关系
        sysUserPostService.deleteByPostIdList(idList);
    }

    @Override
    public List<SysPostVO> postMerchantOrg(SysPostAndOrgQuery query) {
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(query.getMerchantId())) {
            queryWrapper.eq("merchant_id", query.getMerchantId());
        }
        if(StrUtil.isNotBlank(query.getOrgId())) {
            queryWrapper.eq("org_id", query.getOrgId());
        }else if(!ObjectUtils.isEmpty(query.getMerchantId())){
            queryWrapper.isNull("org_id");
        }
        if(StrUtil.isNotBlank(query.getDepartmentId())){
            queryWrapper.eq("department_id", query.getDepartmentId());
        }
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("status", 1);
        return SysPostConvert.INSTANCE.convertList(baseMapper.selectList(queryWrapper));
    }

}